package com.zhouxiaoge.lambda;

import org.junit.Test;

import java.util.*;

/****************************************************************
 卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍
 卐卐卐                    _ooOoo_                    卍卍卍卍卍卍
 卐卐卐      卐           o8888888o          卍       * 佛  卍卍卍
 卐卐卐                  88'' . ''88                  * 祖  卍卍卍
 卐卐卐                  ( | -_- | )                  * 保  卍卍卍
 卐卐卐                   0\  =  /0                   * 佑  卍卍卍
 卐卐卐                ____/'---'\____                *     卍卍卍
 卐卐卐              .'  \\|     |//  '.              * 永  卍卍卍
 卐卐卐             /  \\|||  :  |||//  \             * 无  卍卍卍
 卐卐卐            /  _||||| -:- |||||-  \            * BUG 卍卍卍
 卐卐卐            |   | \\\  -  /// |   |            *     卍卍卍
 卐卐卐            | \_|  ''\---/''  |   |            * 阿  卍卍卍
 卐卐卐            \  .-\__  `-`  ___/-. /            * 弥  卍卍卍
 卐卐卐          ___`. .'  /--.--\  `. . __           * 陀  卍卍卍
 卐卐卐        ."" '<  ‘.___\_<|>_/___.’  >'"".     * 佛  卍卍卍
 卐卐卐       | | :  ` - `.;`\ _ /`;.`/ - ` : | |     卍卍卍卍卍卍
 卐卐卐       | \ `-.   \_ __\ /__ _/   .-`  / /      卍卍卍卍卍卍
 卐卐卐 ======`-.____`-.___\_____/___.-`____.-'=======卍卍卍卍卍卍
 卐@author 周明阳 mail:gqzmy@outlook.com 时间:2019-06-20 16:28卍卍
 卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍
 *
 *
 */

public class Lamdba1Test {

    /**
     * 原匿名内部类
     */
    @Test
    public void oldTest() {
        Comparator<String> comparable = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(), o2.length());
            }
        };
        TreeSet<String> treeSet = new TreeSet<>(comparable);
        TreeSet<String> stringTreeSet = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(), o2.length());
            }
        });
    }

    /**
     * 现Lamdba表达式
     */
    @Test
    public void newTest() {
        Comparator<String> comparator = (x, y) -> Integer.compare(x.length(), y.length());
        TreeSet<String> treeSet = new TreeSet<>(comparator);
        TreeSet<String> stringTreeSet = new TreeSet<>((o1, o2) -> Integer.compare(o1.length(), o2.length()));
    }

    List<Employee> employeeList = Arrays.asList(
            new Employee(101, "张三", 18, 9999.99),
            new Employee(102, "李四", 59, 6666.66),
            new Employee(103, "王五", 38, 7777.77),
            new Employee(104, "赵六", 38, 3333.55),
            new Employee(105, "田七", 44, 5555.55));

    @Test
    public void employeeTest() {
        List<Employee> employees = filterEmployeeAge(employeeList);
        for (Employee employee : employees) {
            System.out.println("年龄小于35的员工信息：" + employee);
        }
        List<Employee> employeeSalary = filterEmployeeSalary(employeeList);
        for (Employee employee : employeeSalary) {
            System.out.println("工资大于5000的员工信息：" + employee);
        }
    }

    //获取公司中年龄小于35的员工信息
    private List<Employee> filterEmployeeAge(List<Employee> employees) {
        List<Employee> employeeList = new ArrayList<>();
        for (Employee employee : employees) {
            if (employee.getAge() <= 35) {
                employeeList.add(employee);
            }
        }
        return employeeList;
    }

    //获取工资大于5000的员工信息
    private List<Employee> filterEmployeeSalary(List<Employee> employees) {
        List<Employee> employeeList = new ArrayList<>();
        for (Employee employee : employees) {
            if (employee.getSalary() >= 5000) {
                employeeList.add(employee);
            }
        }
        return employeeList;
    }


    //优化方式:策略模式
    @Test
    public void filterEmployeeStrategyTes() {
        List<Employee> ageEmployeeList = filterEmployeeStrategy(this.employeeList, new FilterEmployeeForAge<Employee>());
        for (Employee employee : ageEmployeeList) {
            System.out.println("年龄小于35的员工信息：" + employee);
        }
        List<Employee> salaryEmployeeList = filterEmployeeStrategy(this.employeeList, new FilterEmployeeForSalary<Employee>());
        for (Employee employee : salaryEmployeeList) {
            System.out.println("工资大于5000的员工信息：" + employee);
        }
    }

    private List<Employee> filterEmployeeStrategy(List<Employee> employees, FilterEmployeeStrategy<Employee> employeeStrategy) {
        List<Employee> employeeList = new ArrayList<>();
        for (Employee employee : employees) {
            if (employeeStrategy.test(employee)) {
                employeeList.add(employee);
            }
        }
        return employeeList;
    }

    //优化方式二：匿名内部类
    @Test
    public void anonymousInnerClassTest() {
        List<Employee> employeeList = filterEmployeeStrategy(this.employeeList, new FilterEmployeeStrategy<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getId() > 103;
            }
        });
        for (Employee employee : employeeList) {
            System.out.println("员工ID大于103的员工信息：" + employee);
        }
    }

    //优化方式三：Lambda表达式
    @Test
    public void lamdbaTest() {
        List<Employee> ageEmployees = filterEmployeeStrategy(this.employeeList, (e) -> e.getAge() <= 35);
        ageEmployees.forEach(employee -> System.out.println("年龄小于35的员工信息：" + employee));
        List<Employee> salaryEmployees = filterEmployeeStrategy(this.employeeList, e -> e.getSalary() >= 5000);
        salaryEmployees.forEach(System.out::println);
    }

    @Test
    public void streamTest() {
        employeeList.stream().filter(employee -> employee.getAge() <= 35).forEach(employee -> {
            System.out.println("年龄小于35的员工信息：" + employee);
        });
        employeeList.stream().map(Employee::getName).limit(3).sorted().forEach(System.out::println);
    }

    @Test
    public void test1() {
        employeeList.sort((e1, e2) -> {
            if (e1.getAge() == e2.getAge()) {
                return Integer.compare(e2.getId(), e1.getId());
            } else {
                return Integer.compare(e1.getAge(), e2.getAge());
            }
        });
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }
}
